/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events;

import java.util.LinkedList;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GUIDStorage;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;

/**
 * @author Massimo Canonaco
 *
 */
public class MultipleAttacksEvent implements GenericEvent {

	/** */
	static public final float GUID = GUIDStorage.MULTIPLE_ATTACKS_EVENT;
	
	/** */
	private LinkedList<AttackEvent> attacks;
	
	/** */
	private int targetRow;
	private int targetColumn;
	/** */
	public MultipleAttacksEvent(int targetRow, int targetColumn)
	{
		attacks = new LinkedList<>();
		this.targetRow = targetRow;
		this.targetColumn = targetColumn;
	}
	
	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent#getGUID()
	 */
	@Override
	public float getGUID() 
	{
		return GUID;
	}

	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent#getName()
	 */
	@Override
	public String getName() 
	{
	
		return "AREAATTACKEVENT";
	}


	/**
	 * @return the targets
	 */
	public LinkedList<AttackEvent> getAttacks() 
	{
		return attacks;
	}
	
	/** */
	public void add(AttackEvent event)
	{
		attacks.add(event);
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject getAsJSONObject() 
	{
		JSONObject object = new JSONObject();
		object.put(GLOBAL_GUID_KEY, GUID);
		
		JSONArray array = new JSONArray();
		array.add(targetRow);
		array.add(targetColumn);
		
		object.put(TARGET_POINT_KEY, array);
		
		array = new JSONArray();
		for (AttackEvent attack : attacks) 
		{
			array.add(attack.getAsJSONObject());
		}
		object.put(LIST_ATTACKS_KEY, array);
		return object;
	}

	@Override
	public void setFromJSONOnbject(JSONObject object) 
	{
		this.attacks = new LinkedList<>();
		
		JSONArray JSONattacks = (JSONArray)object.get(TARGET_POINT_KEY);
		targetRow = ((Long)JSONattacks.get(0)).intValue();
		targetColumn = ((Long)JSONattacks.get(1)).intValue();
		
		JSONattacks = (JSONArray)object.get(LIST_ATTACKS_KEY);
//		object.
		AttackEvent attackEvent = null;
		for (Object object2 : JSONattacks) 
		{
			attackEvent = new AttackEvent(false, false, "", null, null, 0.0f);
			System.out.println(object2);
			attackEvent.setFromJSONOnbject((JSONObject)JSONValue.parse(object2.toString()));
			this.attacks.add(attackEvent);
		}
			
		System.out.println("resume");
		System.out.println("TARGET: "+targetRow+" "+targetColumn);
		System.out.println("SOURCE: "+attacks.getFirst().getFrom().getRow()+" "+attacks.getFirst().getFrom().getColumn());
		
		
	}

	public int getTargetRow() 
	{
		return targetRow;
	}
	
	public int getTargetColumn() 
	{
		return targetColumn;
	}
	/** */ 
	static private final String LIST_ATTACKS_KEY = "list";
	static private final String TARGET_POINT_KEY = "targetPointLOL";

}
